package com.yf.broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    private EditText et_prefix;
	private Button btn_save;
	private SharedPreferences sp;
	private BroadcastReceiver receiver;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiver = new ScreenLightReceiver();
        // 意图过滤器对象
        IntentFilter filter = new IntentFilter();
        // 给意图添加action
        filter.addAction("android.intent.action.SCREEN_OFF");
        filter.addAction("android.intent.action.SCREEN_ON");
        // 动态注册一个广播接收者
        registerReceiver(receiver, filter);
        
        et_prefix = (EditText) findViewById(R.id.et_prefix);
        btn_save = (Button) findViewById(R.id.btn_save);
        
        sp = getSharedPreferences("voip", MODE_PRIVATE);
        
        btn_save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String prefix = et_prefix.getText().toString().trim();
				if (TextUtils.isEmpty(prefix)) {
					Toast.makeText(MainActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
				} else {
					sp.edit().putString("prefix", prefix).commit();
				}
			}
		});
    }
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//动态注册的广播接收者在当前activity销毁的时候需要注销掉
		unregisterReceiver(receiver);
	}
    
}
